﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using System.Drawing;
using System.Web.UI.DataVisualization.Charting;
namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CompanyofMonth : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();
            }
        }
        void BindList()
        {
            SuperviseQuery query = ViewState["query"] as SuperviseQuery;

            var model = new HZ.Data.QueryModel();
            model["AreaId"] = query.AreaID;
            model["DayTime"] = query.Mid;
            model["CompanyName"] = DAL.DAL_Helper.Replace(txtcompanyname.Text.Trim());

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;
            int t = 0;

            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            DataTable dt = new DataTable();
            if (query.CompanyType == 0)//生产
            {
                dt = iNew.DayTime_SortSore_CompanySC_GroupBy(model, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
            }
            else
            {
                dt = iNew.DayTime_SortSore_CompanyPS_GroupBy(model, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out t);
            }

            AspNetPager1.RecordCount = t;
            //显示相应的字段
            gvwSortInfo.DataSource = dt;

            HZ.Supervise.Web.Function.AddColumn(dt, "SortSorce");   //by张雄    2011.5.6
            HZ.Supervise.Web.Function.ChangeRate(dt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dt, "DistributeRate");

            gvwSortInfo.DataBind();

            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 0);
            HZ.Supervise.Web.Function.Combine(gvwSortInfo, 0, 7);
        }


        /// <summary>
        /// 第一行加日期
        /// 张雄    2011.4.25    modify
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwSortInfo_RowCreated(object sender, GridViewRowEventArgs e)
        {
            #region 合并表头
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:

                    //取得下拉框的月份


                    //第一行表头
                    TableCellCollection tcHeader = e.Row.Cells;
                    tcHeader.Clear();

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[0].Attributes.Add("colspan", "8"); //跨Column
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tcHeader[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    tcHeader[0].Text = FormatDate(sq.Mid) + "</th></tr><tr>";


                    //第2行表头
                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[1].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[1].Text = "企业";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[2].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[2].Text = "采购类型";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[3].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[3].Text = "采购总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[4].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[4].Text = "配送总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[5].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[5].Text = "配送率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[6].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[6].Text = "入库总金额";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[7].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[7].Text = "入库率";

                    tcHeader.Add(new TableHeaderCell());
                    tcHeader[8].Attributes.Add("bgcolor", "Khaki");
                    tcHeader[8].Text = "操作</th></tr><tr>";
                    break;
            }
            #endregion
        }

        /// <summary>
        /// 格式化日期
        /// 张雄 2011.4.25 create
        /// </summary>
        /// <param name="str">未格式化日期参数</param>
        /// <returns>格式化后的日期</returns>
        private string FormatDate(string str)
        {
            //年变量
            string YearStr = String.Empty;
            //月变量
            string MonStr = String.Empty;

            if (!string.IsNullOrEmpty(str))
            {
                if (str.Length == 6)
                {
                    //获取年

                    YearStr = str.Substring(0, 4) + "年";

                    //获取月

                    MonStr = str.Substring(4, 2);

                    if (MonStr.Substring(0, 1) == "0")
                    {
                        MonStr = MonStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonStr += "月";
                    }
                }
            }

            return str = YearStr + MonStr;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void gvwSortInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvwSortInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "CompanyCurrentGoodsList")
            {
                string sessionid = Guid.NewGuid().ToString();
                SuperviseQuery query = ViewState["query"] as SuperviseQuery;

                query.CompanyName_PS = e.CommandArgument.ToString();
                
                query.SortSource = Convert.ToInt32(droSort.SelectedValue);  //从dropSort中取出sortsource存到session中传给下页

                var model = new HZ.Data.QueryModel();
                model["AreaId"] = query.AreaID;    //把AreaId添加到Session的集合中

                Session[sessionid] = query;

                if (!string.IsNullOrEmpty(query.CompanyName_PS))
                {
                    Response.Redirect(string.Format("CompanyCurrentGoodsList.aspx?sessionid={0}", sessionid));
                }
            }
        }

        /// <summary>
        /// 导出excel
        /// 张雄 create 2011.4.27
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            DataTable dtt = new DataTable();

            SuperviseQuery query = ViewState["query"] as SuperviseQuery;

            var model = new HZ.Data.QueryModel();
            model["AreaId"] = query.AreaID;
            model["DayTime"] = query.Mid;
            //model["CompanyName"] = query.CompanyName_PS;

            if (this.txtcompanyname.Text.Trim() != "")
            {
                model["CompanyName"] = txtcompanyname.Text.Trim();
            }

            if (!this.droSort.SelectedValue.Equals("-1"))
            {
                model["SortSorce"] = droSort.SelectedValue;
            }

            model["SortFields"] = this.droSortField.SelectedValue;

            int t = 0;


            HZ.Supervise.IBLL.IBLL_NewStat iNew = HZ.Supervise.BLLFactory.IBLL_NewStats();

            DataTable dt;
            if (query.CompanyType == 0)
            {
                dt = iNew.DayTime_SortSore_CompanySC_GroupBy(model, 1, 0, out t);

            }
            else
            {
                dt = iNew.DayTime_SortSore_CompanyPS_GroupBy(model, 1, 0, out t);
            }


            dtt = dt.Copy();
            HZ.Supervise.Web.Function.AddColumn(dtt, "SortSorce");   //by张雄    2011.5.6
            HZ.Supervise.Web.Function.ChangeRate(dtt, "WarehouseRate");
            HZ.Supervise.Web.Function.ChangeRate_PS(dtt, "DistributeRate");

            HZ.Excel.ExcelHelper.ExportDataForWeb(dtt, this.gvwSortInfo);

        }

    }
}
